var  client = require('../database');
mysql = client.getDbCon();


exports.toAppList = function(req,res){
    var id = req.query['id'];
    var servername = req.query['servername'];
    var selectNameSQL = "SELECT * FROM tb_applist WHERE id IN ( SELECT app_id FROM server_app WHERE server_id = '" + id + "' )";

    mysql.query(selectNameSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            var user = req.session.user;
            return res.render('single_server',{applistdata:data,servername:servername,sid:id,user:user});
        }
    });
};

//获取applist列表的所有值
exports.getAppName = function(req,res){
    var selectNameSQL = "select * from tb_applist";
    mysql.query(selectNameSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            res.end(data);
        }
    });
};

exports.addServer_App = function(req,res){
    var sid = req.body.sid;
    var appid = req.body.appid;
    console.log("sid-----" + sid);
    console.log("appid-----" + appid);
    var insertSQL = 'insert into server_app values(null,' +'"'+ sid +'"' +',' + '"'+appid +'"' +')';

    mysql.query(insertSQL,function(err,rows){
        if (err) console.log(err);
        console.log("INSERT ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};

//检查是否已经存在此appid
exports.hasAppId = function(req,res){
    var sid = req.body.sid;
    var appid = req.body.appid;
    var selectSQL = 'select * from server_app  where server_id=' +'"'+ sid +'"' +'and app_id='+'"'+ appid +'"' ;
    mysql.query(selectSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);

        var returnId = JSON.stringify(rows);
        res.end(returnId);
    });
};

//删除服务关系表中的数据
exports.deleteS_app = function(req,res){
    var sid = req.query['sid'];
    var appid = req.query['appid'];
    console.log("sid---" + sid);
    console.log("appid---" + appid);

    var deleteSQL = 'delete from server_app  where server_id=' +'"'+ sid +'"' +'and app_id='+'"'+ appid +'"' ;
    mysql.query(deleteSQL,function(err,rows){
        if (err) console.log(err);
        console.log("DELETE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};

exports.toPlatform = function(req,res){
    var appid = req.query['appid'];
    var appname = req.query['appname'];
    var servername = req.query['servername'];
    var sid = req.query['sid'];

    var selectSQL= "SELECT * FROM tb_appdetails WHERE id IN (SELECT DISTINCT platformid FROM relation WHERE appid= " + appid +" AND serverid ="+ sid +")";

    mysql.query(selectSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            var user = req.session.user;
            return res.render('platform',{appname:appname,servername:servername,msg:data,appid:appid,user:user,sid:sid});
        }
    });
};

exports.getPlatformName = function(req,res){
    var selectNameSQL = "select * from tb_appdetails";
    mysql.query(selectNameSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            res.end(data);
        }
    });
};

exports.addPlatformName = function(req,res){
    var platformid = req.body.platformid;
    var appid = req.body.appid;
    var sid = req.body.sid;
    var insertSQL = 'insert into relation values(null,'+'"'+ sid +'"' +',' +'"'+ appid +'"' +',' + '"'+platformid +'"' +')';

    mysql.query(insertSQL,function(err,rows){
        if (err) console.log(err);
        console.log("INSERT ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};

exports.hasPlatformId = function(req,res){
    var platformid = req.body.platformid;
    var appid = req.body.appid;
    var sid = req.body.sid;

    var selectSQL = 'select * from relation  where serverid=' +'"'+ sid +'"' +'and appid='+'"'+ appid +'"'+'and platformid='+'"'+ platformid +'"' ;
    mysql.query(selectSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        var returnId = JSON.stringify(rows);
        res.end(returnId);
    });
};

exports.deletePlatform = function(req,res){
    var platformid = req.query['platformid'];
    var appid = req.query['appid'];
    var sid = req.query['sid'];

    var deleteSQL = "delete from relation  where serverid= " + sid  +" and appid= "+ appid +" and platformid= "+ platformid;
    mysql.query(deleteSQL,function(err,rows){
        if (err) console.log(err);
        console.log("DELETE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/toMainPage');
        }
    });
};
